﻿''' <summary>
''' Dieses Objekt fuegt dem Spiel Animationen hinzu, die Zeit versetzt aktiviert werden sollen.
''' </summary>
''' <remarks></remarks>
Public Class BackbufferSpiel
    Private Shared AnimationenLocker As New Mutex
    Private Shared Animationen() As BufferAnimation = {}
    Structure BufferAnimation
        Public Sub New(ByVal AnimationsArt As String, ByVal Frames As Integer, ByVal Position As Point, ByVal Musikspielen As Boolean, ByVal Schlacht As Integer)
            Me.Frames = Frames
            AnimationsArte = AnimationsArt
            Pos = Position
            Musikspielene = Musikspielen
            Me.Schlachte = Schlacht
        End Sub
        Private Frames As Integer
        Public Function Frame() As Boolean
            Frames -= 1
            If Frames <= 0 Then
                Return True
            Else
                Return False
            End If
        End Function
        Private AnimationsArte As String
        Public ReadOnly Property AnimationsArt() As String
            Get
                Return AnimationsArte
            End Get
        End Property

        Private Pos As Point
        Public ReadOnly Property Position() As Point
            Get
                Return Pos
            End Get
        End Property

        Private Musikspielene As Boolean
        Public ReadOnly Property Musikspielen() As Boolean
            Get
                Return Musikspielene
            End Get
        End Property
        Private Schlachte As Integer
        Public ReadOnly Property Schlacht() As Integer
            Get
                Return Schlachte
            End Get
        End Property
    End Structure
    Private Shared Animationen2Locker As New Mutex
    Private Shared Animationen2() As BufferAnimation = {}
    Public Shared Function GetBufferLength() As Integer
        Return Animationen.Length + Animationen2.Length
    End Function
    Public Shared Sub AddAnimation(ByVal Animation As String, ByVal Frames As Integer, ByVal Position As Point, ByVal Musikspielen As Boolean, ByVal Schlacht As Integer)
        If Not Hauptmenue.Aufzeichnungsspulen Then
            ObjektMaus.Visible = False
            Objektverwalter.Spielfeld.EingabenErlauben = False
            Animationen2Locker.Lock()
            Array.Resize(Animationen2, Animationen2.Length + 1)
            Animationen2(Animationen2.Length - 1) = New BufferAnimation(Animation, Frames, Position, Musikspielen, Schlacht)
            Animationen2Locker.Unlock()
        End If
    End Sub

    Public Shared Sub Frame()
        Animationen2Locker.Lock()
        AnimationenLocker.Lock()
        For I = 0 To Animationen2.Length - 1
            Array.Resize(Animationen, Animationen.Length + 1)
            Animationen(Animationen.Length - 1) = Animationen2(I)
        Next
        ReDim Animationen2(-1)
        Dim Buffer2 As BufferAnimation() = {}
        For I = 0 To Animationen.Length - 1
            If Animationen(I).Frame = True Then
                Array.Resize(Objektverwalter.Spielfeld.Animationen, Objektverwalter.Spielfeld.Animationen.Length + 1)
                Objektverwalter.Spielfeld.Animationen(Objektverwalter.Spielfeld.Animationen.Length - 1) = ObjektLadeklasse.Animation(Animationen(I).Position.X, Animationen(I).Position.Y, Animationen(I).AnimationsArt, Animationen(I).Musikspielen, Animationen(I).Schlacht)
            Else
                Array.Resize(Buffer2, Buffer2.Length + 1)
                Buffer2(Buffer2.Length - 1) = Animationen(I)
            End If
        Next
        Animationen = Buffer2
        AnimationenLocker.Unlock()
        Animationen2Locker.Unlock()
    End Sub
    Public Shared Sub DeleteAllAnimations()
        Animationen2Locker.Lock()
        Array.Resize(Animationen2, 0)
        Animationen2Locker.Unlock()
        AnimationenLocker.Lock()
        Array.Resize(Animationen, 0)
        AnimationenLocker.Unlock()
    End Sub
End Class
